package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ContextParamServlet
 */
@WebServlet("/ContextParamServlet")
public class ContextParamServlet extends HttpServlet {

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");   //设置响应编码为utf-8编码
		response.setContentType("text/html");     //设置响应类型为html类型
		PrintWriter out = response.getWriter();   //获取out对象
		out.println("<html>");
		out.println("<head><title>读取上下文参数</title></head>");
		out.println("<body>");
		ServletContext servletContext = this.getServletConfig().getServletContext();  //获取servletContext对象
		String uploadFolder = servletContext.getInitParameter("upload folder");    //获取upload folder参数
		String allowedFileType = servletContext.getInitParameter("allowed file type");  //获取allowed file type参数
		out.println("上传文件夹："+uploadFolder+"<br>");
		out.println("实际磁盘路径："+servletContext.getRealPath(uploadFolder)+"<br>");
		out.println("允许上传文件类型："+allowedFileType);
		out.println("</body>");
		out.println("</html>");
	}
}
